from typing import List
from common.enum.strategy_group_enum import StrategyGroup
from common.enum.strategy_type_enum import StrategyType
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


class DojiAndFiveDaysDowntrendStrategy(BaseStrategy):
    """
    关注策略：分析某个股票昨天是否为十字星，且前5个交易日是否整体下跌，并下跌幅度超过5%。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析股票的昨日是否为十字星，且前5个交易日是否呈整体下跌并下跌幅度超过5%。
        """
        node_point = 0
        description = ""

        # 确保至少有6天的交易数据（昨日 + 前5日）
        if len(trade_info_list) >= 6:
            # 获取昨日数据和前5个交易日数据
            yesterday = trade_info_list[-1]
            last_five_days = trade_info_list[-6:-1]

            # 判断昨日是否为十字星
            is_doji = (
                abs(yesterday.close - yesterday.open) / yesterday.open < 0.005
            )  # 收盘价和开盘价差距小于0.5%
            if is_doji:
                node_point += 2
                description += "昨日为十字星（收盘价接近开盘价）。"

            # 判断前5个交易日是否整体下跌且跌幅超过5%
            first_day = last_five_days[0]
            last_day = last_five_days[-1]
            total_drop = (first_day.close - last_day.close) / first_day.close
            is_downtrend = total_drop > 0.05
            if is_downtrend:
                node_point += 3
                description += f"前5个交易日整体下跌，跌幅为{total_drop:.2%}。"

        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            config=self.strategyConfig(),
            node_point=node_point,
        )

    def strategyConfig(self) -> StrategyInfoMetadata:
        """
        返回策略的配置
        """
        return StrategyInfoMetadata(
            strategy_code="doji_and_five_days_downtrend",
            strategy_name="十字星与五日下跌趋势",
            strategy_group=StrategyGroup.ATTENTION,  # 关注型策略
            strategy_type=StrategyType.PRICE,  # 根据价格类型
            analysis_day=6,
            strategy_level=3,
        )
